/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/8/7 10:46
 */
// 对角线遍历
    // https://leetcode.cn/problems/diagonal-traverse/description/
public class Solution498 {
    public int[] findDiagonalOrder(int[][] mat) {
        int m = mat.length, n = mat[0].length;
        int[] res = new int[m * n];
        if (mat == null || mat.length == 0) return new int[]{};
        int row = 0, column = 0; //两者之和，偶数向上遍历
        for (int i = 0; i < res.length; i++) {
            res[i] = mat[row][column];
            // 向上遍历
            if ((row + column) % 2 == 0) {
                if (column == n - 1) {
                    row++; //准备向下遍历
                } else if (row == 0) {
                    column++; //同样准备向下遍历
                } else {
                    row--;
                    column++;
                }
            } else {
                if (row == m - 1) {
                    column++; // 准备向上遍历
                } else if (column == 0) {
                    row++;
                } else {
                    row++;
                    column--;
                }
            }
        }
        return res;
    }
}
